const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//check phone number
function checkPhoneNumber(phoneNumber) {
  console.log(phoneNumber);

  if (!phoneNumber || phoneNumber.length == 0) {
    wx.showToast({
      title: '手机号不能为空',
      icon: 'none',
    })
    return false;
  }

  if (!phoneNumber.startsWith("1") || phoneNumber.length != 11) {
    wx.showToast({
      title: '手机号格式不正确',
      icon: 'none',
    })
    return false;
  }
  return true;
}

function getLocation(success, fail) {
  return new Promise((success, fail) => {
    wx.getSetting({ //检测用户权限
      success: (res) => {
        if (res.authSetting['scope.userLocation'] == false) { // 地理位置授权 触发过但没有授权
          wx.authorize({
            scope: 'scope.userLocation',
            success() {},
            fail() {
              if (wx.openSetting) {
                openSetting(fail);
              }
              return;
            }
          })
        } else{ // 地理位置授权过了
          wx.getLocation({
            success: function (res) {
              success(res)
            },
            fail:function(res){
              console.log(res);
            }
          })
        }
      }
    })
  });
}

let QQMapWX = require('qqmap-wx-jssdk.min');

function searchPOI(_keyword,latitude, longitude, success, fail) {


  let qqmapsdk = new QQMapWX({
    key: getApp().$QQMapKey // 必填
  });

  return new Promise((success, fail) => {
    qqmapsdk.search({
      keyword: _keyword || '餐饮', //搜索关键词
      location: latitude + ',' + longitude,
      page_size: 12,
      success: function (res) { //搜索成功后的回调
        success(res);
      },
      fail: function (res) {
        fail(res);
      }
    });
  });

}

function openSetting(failCallback) {
  wx.showModal({
    title: '提示', //提示的标题,
    content: '为了能能够正常的使用app,请打开定位功能', //提示的内容,
    showCancel: true, //是否显示取消按钮,
    cancelText: '取消', //取消按钮的文字，默认为取消，最多 4 个字符,
    cancelColor: '#000000', //取消按钮的文字颜色,
    confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
    confirmColor: '#FD7C04', //确定按钮的文字颜色,
    success: res => {
      if (res.confirm) {
        // openSetting 是需要事件驱动的，保证它的同步性。
        wx.openSetting({
          success(res) {},
          fail(res) {}
        })
      } else if (res.cancel) {
        failCallback();
      }
    }
  });
}

function interviewStatus(code) {
  let state = "待面试"
  switch (code) {
    case 0:
      state = "待面试"
      break;
    case 1:
      state = "已取消"
      break;
    case 2:
      state = "未参加面试"
      break;
    case 3:
      state = "不合适"
      break;
    case 4:
      state = "待入职"
      break;
    case 5:
      state = "未入职"
      break;
    case 6:
      state = "已入职"
      break;
    case 7:
      state = "已离职"
      break;

  }
  return state
}

module.exports = {
  formatTime: formatTime,
  checkPhoneNumber: checkPhoneNumber,
  interviewStatus: interviewStatus,
  getLocation: getLocation,
  searchPOI: searchPOI
}